﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class my_addcity : System.Web.UI.Page
{

    ctpDataContext db ;


    protected void Page_Load(object sender, EventArgs e)
    {
        ctp_lib.check_login(this.Page, "my/addcity.aspx", false);

        db = new ctpDataContext();

        if (!this.IsPostBack)
        {
            this.bindmy();
        }

    }
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        this.mybind(0);
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.mybind(1);
    }

    void mybind(int level)
    {

        GeoLingManager glManager = new GeoLingManager(Request, Response);
        String _lang = glManager.getLang();
        DatabaseUtil dbUtil = new DatabaseUtil();
        //Get user's preferred langugage in int
        _lang = dbUtil.getLanguageNo(_lang).ToString();

        var cities = from p in db.ct_profiles
                     select new { title = dbUtil.getPropertyPost(p.title, (int)DatabaseUtil.fromTables.ct_profile_title, p.no, _lang),
                                    continent = p.continent,
                                    country = p.country,
                                    no = p.no};



 
        if (level == 0)
        {
            if (this.DropDownList1.SelectedValue != "-1")
            {

                cities = cities.Where(p => p.continent == int.Parse(this.DropDownList1.SelectedValue));

                this.DropDownList2.Visible = true;

                var contries = from p in cities
                               select new
                               {
                                   p.country,
                                  
                               };

                this.DropDownList2.DataSource = contries.Distinct();
                this.DropDownList2.DataTextField = "country";
                this.DropDownList2.DataValueField = "country";

                this.DataBind();


                this.DropDownList2.Items.Add(new ListItem("Choose Country", "-1"));
                this.DropDownList2.Items.FindByValue("-1").Selected = true;


            }
        }
        else
        {
            if (this.DropDownList2.SelectedItem != null)
            {
                if (this.DropDownList2.SelectedValue != "-1")
                {
                    cities = cities.Where(p => p.country == this.DropDownList2.SelectedValue);
                    this.ListBox1.DataSource = cities;
                    this.ListBox1.DataTextField = "title";
                    this.ListBox1.DataValueField = "no";



                    this.DataBind();


                }
            }

        }





    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (this.ListBox1.SelectedValue == null) return;

        int check = db.my_citiport_mines.Count(p => p.userno == (int)Session["userno"] && p.ct_no == int.Parse(this.ListBox1.SelectedValue));
        if (check != 0) return;

        my_citiport_mine mi = new my_citiport_mine();
        mi.addtime = DateTime.Now;
        mi.ct_no = int.Parse(this.ListBox1.SelectedValue);
        mi.userno =(int) Session["userno"];
        mi.last_update_time = DateTime.Now;

        db.my_citiport_mines.InsertOnSubmit(mi);
        db.SubmitChanges();

        this.bindmy();


    }

    void bindmy()
    {
        GeoLingManager glManager = new GeoLingManager(Request, Response);
        String _lang = glManager.getLang();
        DatabaseUtil dbUtil = new DatabaseUtil();
        //Get user's preferred langugage in int
        _lang = dbUtil.getLanguageNo(_lang).ToString();

        var mi = from p in db.my_citiport_mines
                 join q in db.ct_profiles on p.ct_no equals q.no
                 where p.userno == (int)Session["userno"]
                 orderby p.addtime descending
                 select new
                 {
                     title = dbUtil.getPropertyPost(q.title, (int)DatabaseUtil.fromTables.ct_profile_title, q.no, _lang),
                     q.no

                 };

        this.ListBox2.DataSource = mi;
        this.ListBox2.DataTextField = "title";
        this.ListBox2.DataValueField = "no";

        this.DataBind();
        
        


    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        if (this.ListBox2.SelectedItem == null) return;

        my_citiport_mine mi = db.my_citiport_mines.Single(p => p.userno == (int)Session["userno"] && p.ct_no == int.Parse(this.ListBox2.SelectedValue));
        db.my_citiport_mines.DeleteOnSubmit(mi);
        db.SubmitChanges();

        this.bindmy();

        if (this.ListBox2.Items.Count != 0) this.ListBox2.Items[0].Selected = true;


    }
}
